<!--
 * @Author: YU
 * @Date: 2021-10-24 13:57:03
 * @LastEditTime: 2021-10-31 21:36:24
 * @LastEditors: YU
 * @Description:
-->
<template>
  <el-container class="home">
    <!-- 头部开始 -->
    <el-header class="home_header">
      <div class="header_logo">
        <img src="../assets/whpu.png" />
        <h1>电商后台管理系统</h1>
      </div>
      <div class="header_btn">
        <el-button class="btn_logout" type="info" @click="loginOut"
          >退出</el-button
        >
      </div>
    </el-header>
    <!-- 头部结束 -->

    <!-- 内容部分开始 -->
    <el-container>
      <!-- 侧边栏开始 -->
      <el-aside class="home_aside" :width="isCollapse?'64px':'200px'">
        <div class="aside_scaling" @click="toggleCollapse">|||</div>
        <div class="aside_main">
          <el-menu background-color="#E8FAFF" text-color="#000" active-text-color="#66A7C1" unique-opened
          :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
            <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
              @click="saveNavState('/' + subItem.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </div>
      </el-aside>
      <!-- 侧边栏结束 -->

      <!-- 内容主体开始 -->
      <el-main>
        <!-- 路由占位符-->
        <router-view></router-view>
      </el-main>
      <!-- 内容主体结束 -->
    </el-container>
    <!-- 内容部分结束 -->
  </el-container>
</template>

<script>
import { menuList } from '@/api/home'

export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    // this.activePath = window.sessionStorage.getItem('activePath')
    this.activePath = this.$store.state.activePath
  },
  methods: {
    /**
     * @description:获取左侧菜单列表数据
     * @param {*}
     * @return {*}
     */
    async getMenuList() {
      const { data: res } = await menuList()
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    /**
     * @description: 点击按钮切换菜单的折叠效果
     * @param {*}
     * @return {*}
     */
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    /**
     * @description:保存链接的激活状态
     * @param {*} activePath
     * @return {*}
     */
    saveNavState(activePath) {
      // window.sessionStorage.setItem('activePath', activePath)
      this.$store.commit('setActivePath', activePath)
      this.activePath = activePath
    },
    /**
     * @description: 退出登录
     * @param {*}
     * @return {*}
     */
    loginOut() {
      this.$store.commit('removeUser')
      this.$router.push('/login')
    }
  }
}
</script>

<style lang='scss' scoped>
.home {
  width: 100vw;
  height: 100vh;
  .home_header {
    display: flex;
    justify-content: space-between;
    align-content: center;
    width: 100%;
    height: 60px;
    background-color: #fafeff;
    .header_logo {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      height: 60px;
      margin-left: -20px;
      img {
        width: 190px;
        height: 80%;
        margin-right: 30px;
      }
      h1 {
        font-weight: 400;
        font-size: 20px;
      }
    }
    .header_btn {
      display: flex;
      align-items: center;
      .btn_logout {
        width: 70px;
        height: 40px;
      }
    }
  }

  .home_aside {
    background-color: #e8faff;
    .aside_scaling {
      box-sizing: border-box;
      width: 100%;
      height: 25px;
      font-size: 12px;
      line-height: 25px;
      background-color: #999390;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  }
}
.el-menu {
  border-right: 0;
}
.iconfont {
    margin-right: 10px;
  }
.el-main{
  background-color: #EAEDF1;
}
</style>
